# -*- coding:utf-8 -*-
from django.db import models
import datetime
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

class Poll(models.Model):
    question = models.CharField('Question', max_length=200)
    pub_date = models.DateTimeField('published Date')
    created_by = models.ForeignKey(User)
    ##新增加created_by后，新建polls会出错!!!! form_valid

    def __unicode__(self):
        return self.question

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date < now

    was_published_recently.admin_order_field='pub_date'
    was_published_recently.boolean=True
    was_published_recently.short_description='Published recently?'

    def get_absolute_url(self):
        return reverse('detail', kwargs={'pk': self.pk})


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.choice_text


class Person(models.Model):
    name = models.CharField(max_length=40)
